<!DOCTYPE html>
<html lang="zh-cn">
<head>
  <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  <title>页面性能优化-基于lighthouse - macdfree&#39;s blog</title>
  <meta name="renderer" content="webkit" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>

<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />

<meta name="theme-color" content="#f8f5ec" />
<meta name="msapplication-navbutton-color" content="#f8f5ec">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="#f8f5ec">


<meta name="author" content="macd" /><meta name="description" content="根据Lighthouse进行页面性能优化" />
<meta name="keywords" content="前端性能优化, lighthouse" />



<meta name="baidu-site-verification" content="Ak74Sk61bk" />
<meta name="google-site-verification" content="fAeFHpkrhfFS8LxdzXy0EtAPcVStv0faGn238sRF3L8" />


<meta name="generator" content="Hugo 0.54.0" />


<link rel="canonical" href="/posts/%E9%A1%B5%E9%9D%A2%E6%80%A7%E8%83%BD%E4%BC%98%E5%8C%96-%E5%9F%BA%E4%BA%8Elighthouse/" />

<link rel="apple-touch-icon" sizes="180x180" href="/apple-touch-icon.png">
<link rel="icon" type="image/png" sizes="32x32" href="/favicon-32x32.png">
<link rel="icon" type="image/png" sizes="16x16" href="/favicon-16x16.png">
<link rel="manifest" href="/manifest.json">
<link rel="mask-icon" href="/safari-pinned-tab.svg" color="#5bbad5">




<script async src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>


<link href="/dist/even.min.css?v=3.1.1" rel="stylesheet">
<link href="/lib/fancybox/jquery.fancybox-3.1.20.min.css" rel="stylesheet">




<meta property="og:title" content="页面性能优化-基于lighthouse" />
<meta property="og:description" content="根据Lighthouse进行页面性能优化" />
<meta property="og:type" content="article" />
<meta property="og:url" content="/posts/%E9%A1%B5%E9%9D%A2%E6%80%A7%E8%83%BD%E4%BC%98%E5%8C%96-%E5%9F%BA%E4%BA%8Elighthouse/" />
<meta property="article:published_time" content="2018-03-07T14:13:30&#43;08:00"/>
<meta property="article:modified_time" content="2018-03-08T11:41:30&#43;08:00"/>

<meta itemprop="name" content="页面性能优化-基于lighthouse">
<meta itemprop="description" content="根据Lighthouse进行页面性能优化">


<meta itemprop="datePublished" content="2018-03-07T14:13:30&#43;08:00" />
<meta itemprop="dateModified" content="2018-03-08T11:41:30&#43;08:00" />
<meta itemprop="wordCount" content="1263">



<meta itemprop="keywords" content="前端性能优化,lighthouse," />
<meta name="twitter:card" content="summary"/>
<meta name="twitter:title" content="页面性能优化-基于lighthouse"/>
<meta name="twitter:description" content="根据Lighthouse进行页面性能优化"/>

<!--[if lte IE 9]>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/classlist/1.1.20170427/classList.min.js"></script>
<![endif]-->

<!--[if lt IE 9]>
  <script src="https://cdn.jsdelivr.net/npm/html5shiv@3.7.3/dist/html5shiv.min.js"></script>
  <script src="https://cdn.jsdelivr.net/npm/respond.js@1.4.2/dest/respond.min.js"></script>
<![endif]-->

</head>
<body>
  <div id="mobile-navbar" class="mobile-navbar">
  <div class="mobile-header-logo">
    <a href="/" class="logo">macdfree&#39;s blog</a>
  </div>
  <div class="mobile-navbar-icon">
    <span></span>
    <span></span>
    <span></span>
  </div>
</div>
<nav id="mobile-menu" class="mobile-menu slideout-menu">
  <ul class="mobile-menu-list">
    <a href="/">
        <li class="mobile-menu-item">首页</li>
      </a><a href="/categories/">
        <li class="mobile-menu-item">分类</li>
      </a><a href="/tags/">
        <li class="mobile-menu-item">标签</li>
      </a><a href="/archive/">
        <li class="mobile-menu-item">归档</li>
      </a>
  </ul>
</nav>
  <div class="container" id="mobile-panel">
    <header id="header" class="header">
        <div class="logo-wrapper">
  <a href="/" class="logo">macdfree&#39;s blog</a>
</div>

<nav class="site-navbar">
  <ul id="menu" class="menu">
    <li class="menu-item">
        <a class="menu-item-link" href="/">首页</a>
      </li><li class="menu-item">
        <a class="menu-item-link" href="/categories/">分类</a>
      </li><li class="menu-item">
        <a class="menu-item-link" href="/tags/">标签</a>
      </li><li class="menu-item">
        <a class="menu-item-link" href="/archive/">归档</a>
      </li>
  </ul>
</nav>
    </header>

    <main id="main" class="main">
      <div class="content-wrapper">
        <div id="content" class="content">
          <article class="post">
    
    <header class="post-header">
      <h1 class="post-title">页面性能优化-基于lighthouse</h1>

      <div class="post-meta">
        <span class="post-time"> 2018-03-07 </span>
        <div class="post-category">
          
            <a href="/categories/html/"> html </a>
          
        </div>
        <span class="more-meta"> 约 1263 字 </span>
        <span class="more-meta"> 预计阅读 3 分钟 </span>
        <span id="busuanzi_container_page_pv" class="more-meta"> <span id="busuanzi_value_page_pv"><img src="/img/spinner.svg" alt="spinner.svg"/></span> 次阅读 </span>
      </div>
    </header>

    
    
<div class="post-toc" id="post-toc">
  <h2 class="post-toc-title">文章目录</h2>
  
  <div class="post-toc-content always-active">
    <nav id="TableOfContents">
<ul>
<li><a href="#前期准备">前期准备</a>
<ul>
<li><a href="#lighthouse安装">Lighthouse安装</a></li>
<li><a href="#lighthouse使用">Lighthouse使用</a></li>
</ul></li>
<li><a href="#检测点修改">检测点修改</a>
<ul>
<li><a href="#enable-text-compression">Enable text compression</a></li>
<li><a href="#serve-images-in-next-gen-formats">Serve images in next-gen formats</a></li>
<li><a href="#reduce-render-blocking-scripts-stylesheets">Reduce render-blocking scripts &amp; stylesheets</a></li>
<li><a href="#unused-css-rules">Unused CSS rules</a></li>
</ul></li>
<li><a href="#总结">总结</a></li>
</ul>
</nav>
  </div>
</div>

    
    <div class="post-content">
      <p>最近使用GoogleChrome的Lighthouse插件对公司的登录页面进行了检测优化，在此记录一下具体优化过程。</p>

<h1 id="前期准备">前期准备</h1>

<h2 id="lighthouse安装">Lighthouse安装</h2>

<p>使用Lighthouse有两种方式：Chrome扩展程序或命令行工具，本文只介绍以Chrome扩展程序方式使用Lighthouse。</p>

<ul>
<li>适用的Chrome版本：52或更高版本。</li>
<li>安装<a href="https://chrome.google.com/webstore/detail/lighthouse/blipmdconlkpinefehnmjammfjpmpbjk">Lighthouse扩展程序</a>。</li>
</ul>

<h2 id="lighthouse使用">Lighthouse使用</h2>

<p>Lighthouse扩展程序安装完成后，打开需要检测的页面，点击右上角的插件图标再点击 “Generate report” 按钮开始进行检测。</p>

<p><img src="/images/Lighthouse插件.png" alt="Lighthouse插件" /></p>

<p>检测完成后，插件会自动打开检测报告页面，如下图所示：</p>

<p><img src="/images/旧页面性能截图.png" alt="旧页面性能截图" /></p>

<p>上图是测试系统登录页面的性能得分，可以看到，分数极低。下面就对照检测点，进行修改。</p>

<h1 id="检测点修改">检测点修改</h1>

<h2 id="enable-text-compression">Enable text compression</h2>

<p><img src="/images/gzip压缩.png" alt="gzip压缩" /></p>

<p>点开详细信息，可以看到测试系统没有使用gzip压缩。</p>

<p>由于测试系统使用Nginx代理，所以直接添加Nginx配置，如下：</p>

<pre><code class="language-nginx">gzip on;
gzip_min_length 3k;
gzip_buffers  4 4k;
gzip_comp_level 3;
gzip_types text/plain application/x-javascript application/javascript text/css application/xml text/javascript application/x-httpd-php image/jpeg image/gif image/png;
gzip_vary on;
</code></pre>

<ul>
<li><p>gzip_min_length <em>length</em>;</p>

<p>只有当响应头中的<code>Content-Length</code>大于<code>length</code>时才进行压缩，实际测试中发现调整到 3k 比较合适</p></li>

<li><p>gzip_buffers <em>number size</em>;</p>

<p>设置用于压缩响应数据的内存大小，默认size等于一个内存页的大小，在Linux中可以使用命令<code>getconf PAGESIZE</code>进行查看</p></li>

<li><p>gzip_comp_level <em>level</em>;</p>

<p>设置压缩级别，<del>按照我的理解，级别越高表示压缩率越高体积越小，但同时压缩效率也越低</del>。</p></li>

<li><p>gzip_types <em>mime-type &hellip;</em>;</p>

<p>设置指定哪些MIME type需要被压缩，值<code>&quot;*&quot;</code>表示匹配任意MIME type。此外，<code>&quot;text/html&quot;</code>类型总是被压缩的。</p></li>

<li><p>gzip_vary <em>on | off</em>;</p>

<p>当启用<code>gzip</code>，<code>gzip_static</code> 或 <code>gunzip</code>是是否在响应头中插入<code>Vary: Accept-Encoding</code>属性。</p></li>
</ul>

<h2 id="serve-images-in-next-gen-formats">Serve images in next-gen formats</h2>

<p><img src="/images/图片文件过大.png" alt="图片过大" /></p>

<p>检查点建议使用新的图片格式以提高压缩率，减小图片体积。针对此点，考虑到兼容性问题，本人并没有使用建议的图片格式，而是将png格式转成了jpg格式，从原来的 341 KB 缩小到 55.7 KB，虽然png是无损压缩，但对于登录页大背景图片来说jpg才是推荐的图片格式。</p>

<h2 id="reduce-render-blocking-scripts-stylesheets">Reduce render-blocking scripts &amp; stylesheets</h2>

<p><img src="/images/阻塞的js和css.png" alt="阻塞的js和css" /></p>

<p>检测点中列出的js和css都是在<code>&lt;head&gt;</code>标签中，浏览器会等待这些css和js都加载完毕后再渲染页面，从而导致页面加载速度下降。本人采取的做法是将真正需要在页面渲染前加载的部分js和css使用<code>&lt;script&gt;</code>和<code>&lt;style&gt;</code>标签写在页面上，这样可以减少页面渲染前的资源加载。</p>

<h2 id="unused-css-rules">Unused CSS rules</h2>

<p><img src="/images/无用的css.png" alt="无用的css" /></p>

<p>这个检测点比较简单，需要注意的是列出的css并不是真的都是没有用到的，需要人工核查。</p>

<h1 id="总结">总结</h1>

<p>通过检测点的修改，我们大致可以明确提升页面性能的几个目标：</p>

<ol>
<li>减小数据大小</li>
<li>减少资源加载数量</li>
<li>设法降低页面首次渲染完成的时间</li>
</ol>

<p>其中前两点可以认为是为第三点服务的，但要实现第三点，不仅需要完成前两点，还需要针对具体页面，分析梳理页面关键资源，对于非关键资源采用延迟加载，以提高页面加载速度。</p>

<p>另外，还可以使用HTTP/2的server push功能在一次请求中返回多个资源文件，从而减少HTTP请求次数，提高页面性能。</p>
    </div>

    
    <div class="post-copyright">
  <p class="copyright-item">
    <span class="item-title">文章作者</span>
    <span class="item-content">macd</span>
  </p>
  <p class="copyright-item">
    <span class="item-title">上次更新</span>
    <span class="item-content">2018-03-08</span>
  </p>
  
  
</div>

    
    
<div class="post-reward">
  <input type="checkbox" name="reward" id="reward" hidden />
  <label class="reward-button" for="reward">赞赏支持</label>
  <div class="qr-code">
    
    
      <label class="qr-code-image" for="reward">
        <img class="image" src="/img/reward/wechat-qr-code.png">
        <span>微信打赏</span>
      </label>
    
      <label class="qr-code-image" for="reward">
        <img class="image" src="/img/reward/alipay-qr-code.png">
        <span>支付宝打赏</span>
      </label>
  </div>
</div>

    <footer class="post-footer">
      
        <div class="post-tags">
          
          <a href="/tags/%E5%89%8D%E7%AB%AF%E6%80%A7%E8%83%BD%E4%BC%98%E5%8C%96/">前端性能优化</a>
          
          <a href="/tags/lighthouse/">lighthouse</a>
          
        </div>

      
      <nav class="post-nav">
        
          <a class="prev" href="/posts/%E5%BF%AB%E9%80%9Fssh%E8%BF%9E%E6%8E%A5%E5%B7%A5%E5%85%B7-%E8%87%AA%E7%94%A8/">
            <i class="iconfont icon-left"></i>
            <span class="prev-text nav-default">快速SSH连接工具（自用）</span>
            <span class="prev-text nav-mobile">上一篇</span>
          </a>
        
          <a class="next" href="/posts/sublimetext%E6%8F%92%E4%BB%B6%E7%BC%96%E5%86%99%E5%A4%87%E5%BF%98/">
            <span class="next-text nav-default">SublimeText插件编写备忘</span>
            <span class="prev-text nav-mobile">下一篇</span>
            <i class="iconfont icon-right"></i>
          </a>
      </nav>
    </footer>
  </article>
        </div>
        
  <div id="comments-gitment"></div>
  <link rel="stylesheet" href="/lib/gitment/gitment-0.0.3.min.css">
    <script src="/lib/gitment/gitment-0.0.3.min.js"></script>
  <script type="text/javascript">
  const gitment = new Gitment({
    id: '2018-03-07 14:13:30 \x2b0800 \x2b0800',
    title: '页面性能优化-基于lighthouse',
    link: decodeURI(location.href),
    desc: '\x3cp\x3e最近使用GoogleChrome的Lighthouse插件对公司的登录页面进行了检测优化，在此记录一下具体优化过程。\x3c\/p\x3e',
    owner: 'MACDfree',
    repo: 'gitment4',
    oauth: {
      client_id: '3de7eda9fae832753b25',
      client_secret: 'fb1c82c44491295195db0eb7a2d12af22dfcae29'
    }
  })
  gitment.render('comments-gitment')
  </script>
  <noscript>Please enable JavaScript to view the <a href="https://github.com/imsun/gitment">comments powered by gitment.</a></noscript>
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="social-links">
      <a href="mailto:macdfree@163.com" class="iconfont icon-email" title="email"></a>
      <a href="https://github.com/MACDfree" class="iconfont icon-github" title="github"></a>
  <a href="/index.xml" type="application/rss+xml" class="iconfont icon-rss" title="rss"></a>
</div>

<div class="copyright">
  <span class="power-by">
    由 <a class="hexo-link" href="https://gohugo.io">Hugo</a> 强力驱动
  </span>
  <span class="division">|</span>
  <span class="theme-info">
    主题 - 
    <a class="theme-link" href="https://github.com/olOwOlo/hugo-theme-even">Even</a>
  </span>

  <div class="busuanzi-footer">
    <span id="busuanzi_container_site_pv"> 本站总访问量 <span id="busuanzi_value_site_pv"><img src="/img/spinner.svg" alt="spinner.svg"/></span> 次 </span>
    <span class="division">|</span>
    <span id="busuanzi_container_site_uv"> 本站总访客数 <span id="busuanzi_value_site_uv"><img src="/img/spinner.svg" alt="spinner.svg"/></span> 人 </span>
  </div>

  <span class="copyright-year">
    &copy; 
    
      2014 - 
    2019
    <span class="heart">
      <i class="iconfont icon-heart"></i>
    </span>
    <span class="author">macd</span>
    <span class="division">|</span>
    <span style="margin-left: 5px;">
      <img alt="buildstatus" src="https://travis-ci.org/MACDfree/hugoblog.svg?branch=master">
    </span>
  </span>
</div>
    </footer>

    <div class="back-to-top" id="back-to-top">
      <i class="iconfont icon-up"></i>
    </div>
  </div>
  
<script src="/lib/highlight/highlight.pack.js?v=20171001"></script><script type="text/javascript" src="/lib/jquery/jquery-3.2.1.min.js"></script>
  <script type="text/javascript" src="/lib/slideout/slideout-1.0.1.min.js"></script>
  <script type="text/javascript" src="/lib/fancybox/jquery.fancybox-3.1.20.min.js"></script>
<script type="text/javascript" src="/dist/even.min.js?v=3.1.1"></script>








</body>
</html>
